package dao

import (
	"context"
	"encoding/json"

	"github.com/mjcode-max/TurboGin/config"
	"github.com/mjcode-max/TurboGin/pkg/logger"
	"github.com/mjcode-max/TurboGin/pkg/redis"
)

type RankDAO struct {
	redisClient *redis.Client
}

func NewRankService(redisClient *redis.Client) *RankDAO {
	return &RankDAO{redisClient: redisClient}
}
func (s *RankDAO) ProvinceSkinsList(ctx context.Context, code string) (string, error) {

	cfg := config.LoadConfig()
	logger, err := logger.New(cfg)
	if err != nil {
		return "", err
	}
	redisClient, _ := redis.New(cfg, logger)

	client := redisClient.GetClient()

	key := "province:skins:" + code
	result, err := client.SMembers(ctx, key).Result()
	if err != nil {
		return "", err
	}
	jsonData, err := json.Marshal(result)
	if err != nil {
		return "", err
	}
	return string(jsonData), nil
}

// ProvinceRankList 获取省份排行数据
func (s *RankDAO) ProvinceRankList(ctx context.Context) (string, error) {
	// 1. 准备配置和日志
	cfg := config.LoadConfig()     // 从 config.yaml 加载配置
	logger, err := logger.New(cfg) // 修正变量名并处理错误
	if err != nil {
		return "", err
	}
	// 2. 创建 Redis 客户端
	redisClient, _ := redis.New(cfg, logger)

	// 3. 获取原生客户端
	client := redisClient.GetClient()

	// 4. 从 Redis 获取数据
	key := "province:rank:1001" // 替换为你的 Redis Key
	result, err := client.SMembers(ctx, key).Result()
	if err != nil {
		return "", err
	}
	jsonData, err := json.Marshal(result)
	if err != nil {
		return "", err
	}
	return string(jsonData), nil
}

func NewRankDAO() *RankDAO {
	r := redis.NewRedisClient()
	return &RankDAO{redisClient: r}
}
